//
//  CBCMenuView.m
//  CBCReader
//
//  Created by Labanotation on 16/6/1.
//  Copyright © 2016年 okwei. All rights reserved.
//

#import "CBCMenuView.h"
#import "CBCTopMenuView.h"
#import "CBCBottomMenuView.h"
#import "CBCReaderDef.h"
#if __has_include(<Masonry/Masonry.h>)
#import <Masonry/Masonry.h>
#else
#import "Masonry.h"
#endif
//#define TopViewHeight  CBCiPhoneX?92.0f:70.0f
#define TopViewHeight  (CBCiPhoneX?94.0f:70.0f)
#define AnimationDelay 0.3f
#define BottomViewHeight (CBCiPhoneX?130.0f:110.0f)
@interface CBCMenuView ()<CBCMenuViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic,strong) UITapGestureRecognizer * tap;
@property(nonatomic,assign) BOOL isShow;

@end
@implementation CBCMenuView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
        
    }
    return self;
}

-(void)setIsHiddenShare:(BOOL)isHiddenShare{
    _isHiddenShare = isHiddenShare;
    _topView.isHiddenShare = _isHiddenShare;
}
-(void)setup
{
    [self addSubview:self.topView];
    [self addSubview:self.bottomView];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(TopViewHeight);
        make.top.equalTo(self).offset(-TopViewHeight);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(BottomViewHeight);
        make.bottom.equalTo(self).offset(BottomViewHeight);
    }];
    
    [self addGestureRecognizer:self.tap];
}

-(CBCTopMenuView *)topView
{
    if (!_topView) {
        _topView = [[CBCTopMenuView alloc] init];
        _topView.delegate = self;
    }
    return _topView;
}

-(CBCBottomMenuView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[CBCBottomMenuView alloc] init];
        _bottomView.delegate = self;
    }
    return _bottomView;
}
-(void)setRecordModel:(CBCRecordModel *)recordModel
{
    _recordModel = recordModel;
    _bottomView.readModel = recordModel;
}
#pragma mark - CBCMenuViewDelegate

-(void)menuViewInvokeCatalog:(CBCBottomMenuView *)bottomMenu
{
    if ([self.delegate respondsToSelector:@selector(menuViewInvokeCatalog:)]) {
        [self.delegate menuViewInvokeCatalog:bottomMenu];
    }
}
-(void)menuViewJumpChapter:(NSUInteger)chapter page:(NSUInteger)page
{
    if ([self.delegate respondsToSelector:@selector(menuViewJumpChapter:page:)]) {
        [self.delegate menuViewJumpChapter:chapter page:page];
    }
}
-(void)menuViewFontSize:(CBCBottomMenuView *)bottomMenu
{
    if ([self.delegate respondsToSelector:@selector(menuViewFontSize:)]) {
        [self.delegate menuViewFontSize:bottomMenu];
    }
}
-(void)menuViewMark:(CBCTopMenuView *)topMenu
{
    if ([self.delegate respondsToSelector:@selector(menuViewMark:)]) {
        [self.delegate menuViewMark:topMenu];
    }
}
-(void)menuViewShare:(CBCTopMenuView *)topMenu
{
    if ([self.delegate respondsToSelector:@selector(menuViewShare:)]) {
        [self.delegate menuViewShare:topMenu];
    }
}
-(void)menuViewBack:(CBCTopMenuView *)topMenu
{
    if ([self.delegate respondsToSelector:@selector(menuViewBack:)]) {
        [self.delegate menuViewBack:topMenu];
    }
}
-(void)menuViewChapterProgress:(CGFloat)progress{
    if (self.delegate&&[self.delegate respondsToSelector:@selector(menuViewChapterProgress:)]) {
        [self.delegate menuViewChapterProgress:progress];
    }
}

#pragma mark -


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if (touch.view == self) {
        return YES;
    }
    return NO;
    
    
}

-(void)hiddenSelf:(UITapGestureRecognizer*)tap{
    if (self.isShow) {
        [self hiddenAnimation:YES];
    }
}
-(void)showAnimation:(BOOL)animation
{
    if (self.isShow) {
        return;
    }
    self.hidden = NO;
    self.isShow = YES;
    [_bottomView resetUI];
    _bottomView.currentProgress = self.currentProgress;
    [_topView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
    }];
    [_bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
    }];
    [UIView animateWithDuration:animation?AnimationDelay:0 animations:^{
        
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
    if ([self.delegate respondsToSelector:@selector(menuViewDidAppear:)]) {
        [self.delegate menuViewDidAppear:self];
    }
}
-(void)hiddenAnimation:(BOOL)animation
{
  
    [UIView animateWithDuration:animation?AnimationDelay:0 animations:^{
        [_topView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(-TopViewHeight);
        }];
        [_bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(BottomViewHeight);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.hidden = YES;
        self.isShow = NO;
        
    }];
    
    if ([self.delegate respondsToSelector:@selector(menuViewDidHidden:)]) {
        [self.delegate menuViewDidHidden:self];
    }
}

-(UITapGestureRecognizer *)tap{
    if (!_tap) {
        _tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenSelf:)];
        _tap.delegate = self;
    }return _tap;
}

@end
